package cn.caidabao.gb.component.sip.server.request.send;

import cn.caidabao.gb.bean.GBSipRegisterSession;
import cn.caidabao.gb.bean.InviteObject;
import cn.caidabao.gb.bean.OGSipCallIdCallBack;
import cn.caidabao.gb.component.sip.CustomSipServer;
import cn.caidabao.gb.component.sip.server.storage.CallBackStorage;
import lombok.SneakyThrows;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
public class ByeMessageSender extends RequestSender<InviteObject,Boolean>{
    @Resource
    private CallBackStorage callBackStorage;
    @SneakyThrows
    @Override
    public OGSipCallIdCallBack<Boolean> sendAsync(GBSipRegisterSession gbSipRegisterSession, InviteObject extraData) {
        OGSipCallIdCallBack callBackByChannelId = callBackStorage.getAndDeleteCallBackByChannelId(extraData.getChanelId());
        String callId = callBackByChannelId.getCallId();
        OGSipCallIdCallBack<Boolean> callBack = new OGSipCallIdCallBack<>(callId);
        callBackStorage.putCallBack(callId,callBack);
        CustomSipServer.server.sendByeMessage(gbSipRegisterSession.getDeviceId(),
                extraData.getChanelId(),
                gbSipRegisterSession.getIp()+":"+gbSipRegisterSession.getPort(),
                callBackByChannelId.getCallId(),callBackByChannelId.getCSeq()+1 );
        return callBack;
    }
}
